package com.kornero.auto.view.groups;

import com.kornero.auto.util.view.LayoutManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MainGroupImpl extends Composite implements IMainGroup {

    private static final Logger logger = LoggerFactory.getLogger(MainGroupImpl.class);

    private static final String MESSAGE = "Message: ";
    private static final String CONNECTION_STATUS = "Connection status: ";

    private static final int DEFAULT_OLD = 7;

    private Label messages;
    private Label connectionStatus;
    private ProgressBar progressBar;
    private Button request;
    private Button clearDB;
    private Button clearOld;
    private Text old;

    public MainGroupImpl(Composite parent) {
        this(parent, SWT.NONE);
    }

    public MainGroupImpl(Composite parent, int style) {
        super(parent, style);
        this.constructUI();
    }

    private void constructUI() {
        this.setLayout(new FillLayout());

        final Group group = new Group(this, SWT.NONE);
        group.setLayout(LayoutManager.getRowVerticalLayout());
        group.setText("Main");

        this.messages = createMessagesLabel(group);
        this.connectionStatus = createConnectionStatusLabel(group);

        final Composite composite0 = new Composite(group, SWT.NONE);
        composite0.setLayout(new RowLayout());

        final Composite composite1 = new Composite(composite0, SWT.NONE);
        composite1.setLayout(LayoutManager.getFillVerticalLayout());

        final Composite composite2 = new Composite(composite1, SWT.NONE);
        composite2.setLayout(new FillLayout());

        this.request = createRequestButton(composite2);
        this.clearDB = createClearDbButton(composite2);

        this.progressBar = new ProgressBar(composite1, SWT.SMOOTH);

        final Composite composite3 = new Composite(composite0, SWT.NONE);
        composite3.setLayout(LayoutManager.getFillVerticalLayout());

        this.clearOld = new Button(composite3, SWT.NONE);
        clearOld.setText("Delete all older than");

        final Composite composite4 = new Composite(composite3, SWT.NONE);
        composite4.setLayout(new FillLayout());
        this.old = new Text(composite4, SWT.BORDER);
        old.setText(String.valueOf(DEFAULT_OLD));
        new Label(composite4, SWT.NONE).setText(" days");

        group.pack();
    }

    private static Label createMessagesLabel(final Composite composite) {
        final Label label = new Label(composite, SWT.NONE);
        final String shift = "            ";
        label.setText(MESSAGE + shift + shift + shift + shift + shift + shift);
        return label;
    }

    private static Label createConnectionStatusLabel(final Composite composite) {
        final Label label = new Label(composite, SWT.NONE);
        label.setText(CONNECTION_STATUS);
        return label;
    }

    private static Button createRequestButton(final Composite composite) {
        final Button button = new Button(composite, SWT.NONE);
        button.setText("Do request");
        return button;
    }

    private static Button createClearDbButton(final Composite composite) {
        final Button button = new Button(composite, SWT.NONE);
        button.setText("Clear DB");
        return button;
    }

    @Override
    public void addRequestListener(final Listener listener) {
        request.addListener(SWT.MouseDown, listener);
    }

    @Override
    public void addClearDBListener(final Listener listener) {
        clearDB.addListener(SWT.MouseDown, listener);
    }

    @Override
    public void addClearOldListener(final Listener listener) {
        clearOld.addListener(SWT.MouseDown, listener);
    }

    @Override
    public void resetProgressBar() {
        progressBar.setSelection(0);
    }

    @Override
    public void setUpProgressBar(final int max) {
        progressBar.setMaximum(max);
    }

    @Override
    public void incProgressBar() {
        progressBar.setSelection(progressBar.getSelection() + 1);
    }

    @Override
    public void setMessage(String message) {
        final String text = MESSAGE + message;
        final int max = (text.length() > 100) ? 100 : text.length();
        this.messages.setText(text.substring(0, max));
        this.messages.pack();
    }

    @Override
    public void setConnectionStatus(String status) {
        final String text = CONNECTION_STATUS + status;
        final int max = (text.length() > 100) ? 100 : text.length();
        this.connectionStatus.setText(text.substring(0, max));
        this.connectionStatus.pack();
    }

    @Override
    public int getDaysOld() {
        try {
            final int daysOld = Integer.parseInt(this.old.getText());
            if (daysOld > 0) {
                return daysOld;
            }
        } catch (NumberFormatException e) {
            logger.error("getTimeout(): Bad number = '" + this.old.getText() + "'");
        }

        // One week by default.
        this.old.setText(String.valueOf(DEFAULT_OLD));
        return DEFAULT_OLD;
    }
}